$(function(){
    var c_city=Utils.getCookie("currentCity");
    var l_city=Utils.getCurrentCity("currentCity");
    var curr_city=document.getElementById("currentCity");
    var phoneNO;
    var hasLogin=false;//默认设置false未登录,true已登录
    var url=Utils.reqUrl,
        async=true,
        type="post";
    var requestData="";
    var userNo= Utils.getCookie("userNo");
    var pwd= Utils.getCookie("password");
    if(pwd){
        $("[name='checkbox']").attr("checked", "true");
    }
    if (userNo) {//用户名存在的话就把用户名填充到用户名文本框
        $(".login-name .login-input").val(userNo);
    }
    if (pwd) {//密码存在的话把密码填充到密码文本框
        $(".login-pwd .login-input").val(pwd);
    }
    var password=$(".login-pwd .login-input").val();
    function getCurr_city(){
        var  reqUrl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
        function callback(){
            var city=remote_ip_info.city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
        }
        //如果已经定位过,直接设置为已定位的，否则重新自动定位
        if(c_city||l_city){
            var city=l_city||c_city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
        }else{
            $.getScript(reqUrl,callback);
        }

        $(".changecity a").on("click",function(){
            location.href="./address.html?currentCity="+curr_city.innerHTML;
        });
    }
    //定位城市
    getCurr_city();
    function getData(url,datas,call_func,async,type){
        console.log("请求参数"+datas);
        $.ajax({
            url:url,
            type:type,
            datatype:"json",
            async:async,
            contentType:"application/json",
            data:datas,
            success:function(data){
                console.log(data);
                if(data.statu==0){//登录成功
                    call_func(data);        
                }else{//登录异常
                    $(".error").css("display","block");
                    $(".error").html(data.msg);    
                }  
            },
            error:function(msg){
                console.log(msg);
            }   
        });        
    }
    //登录
    function handdle_func1(data){
        var userName=data.data.user.userName;//用户名
        var userId=data.data.user.userId;//用户id
        var shopId=data.data.seller.shopId;//商家id
        if(data.msg=="登录成功"){
            hasLogin=true;//登录成功
            Utils.setCookie("shopId",shopId);
            Utils.setCookie("hasLogin",hasLogin);//已登录标志
            Utils.setCookie("userName",userName);//用户名
            Utils.setCookie("userId",userId);//用户id
            if($(".auto-login").attr("checked")){//记住密码
                Utils.setCookie("userNo",phoneNO);
                Utils.setCookie("password",password);//登录成功
            }else{
                Utils.delCookie("userNo");
                Utils.delCookie("password");
            }
            var a=Utils.getCookie("currentCity");
            var b=Utils.getCurrentCity("currentCity");
            console.log("当前currentCity"+a+"b:"+b);
            location.href = "./index.html?currentCity="+curr_city.innerHTML+"&s_d="+shopId;//登录成功跳转到首页
        }else{
            $(".error").css("display","block");
            $(".error").html("账号或者密码错误！");
        }
    }
    //手动输入加密
    $(".login-pwd .login-input").change(function(){
          password=$(".login-pwd .login-input").val();
          password=hex_md5(password);
    });
    $(".loginIn").on("click",function(){
        phoneNO=$(".login-name .login-input").val();
        var p_wd=$(".login-pwd .login-input").val();
         if($.trim(p_wd)=="" || $.trim(phoneNO)==""){
              $(".error").css("display","block");
        }else{
               $(".error").css("display","none");
        }
        requestData=JSON.stringify({
            "code":"login",
            "version":"1.0",
            "data":{
                "phoneNO":phoneNO,
                "password":password
            }
        });
         getData(url,requestData,handdle_func1,async,type);
    });
});